package widget;

import android.animation.ValueAnimator;
import android.app.ActionBar;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.*;
import com.dispatchapp.andlei.R;
import jp.wasabeef.richeditor.RichEditor;


/**
 * 更新弹窗
 * Created by Andlei on 2019/2/25.
 */

public class UpdatePopupWindow extends PopupWindow {
    private Context mContext;
    private final View mView;
    private RelativeLayout mLlContent;
    private RichEditor tv_update_content;
    private FlikerProgressBar progressBar;
    private Button btn_shengji;
    private ImageView img_cancle;
    private String content;
    public UpdatePopupWindow(Context context, String content, String type, View.OnClickListener onClickListener , View.OnKeyListener onKeyListener) {
        super(context);
        this.mContext = context;
        this.content = content;
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = inflater.inflate(R.layout.popupwindow_update,null);
        initView(mView);
        btn_shengji.setOnClickListener(onClickListener);
        mLlContent.setOnKeyListener(onKeyListener);
        if(type.equals("2")){
            img_cancle.setVisibility(View.GONE);
        }else {
            img_cancle.setVisibility(View.VISIBLE);
        }
    }

    private void initView(View view) {
        mLlContent = view.findViewById(R.id.pop_layout);

        this.setContentView(mView);
        this.setWidth(ActionBar.LayoutParams.MATCH_PARENT);
        this.setHeight(ActionBar.LayoutParams.MATCH_PARENT);
        this.setAnimationStyle(R.style.AppTheme);
        mView.setFocusable(true); // 这个很重要
        mView.setFocusableInTouchMode(true);
        this.setFocusable(false);
        this.setOutsideTouchable(false);
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        this.setBackgroundDrawable(dw);

//        mView.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                int height = mView.findViewById(R.id.pop_layout).getTop();
//                int y = (int) event.getY();
//                if (event.getAction() == MotionEvent.ACTION_UP) {
//                    if (y < height) {
//                        hiddenPopupWindow();
//                    }
//                }
//                return true;
//            }
//        });
        btn_shengji = mView.findViewById(R.id.btn_shengji);
        img_cancle = mView.findViewById(R.id.img_cancle);
        tv_update_content = mView.findViewById(R.id.tv_update_content);
        tv_update_content.setHtml(content);
        tv_update_content.setInputEnabled(false);
        //tv_update_content.setText(Html.fromHtml(content));
        progressBar = mView.findViewById(R.id.progress_bar);
        img_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
    }
    public FlikerProgressBar GetProgressBar(){
        return progressBar;
    }
    public RichEditor Gettv_update_content(){
        return tv_update_content;
    }
    public Button Getbtn_shengji(){
        return btn_shengji;
    }
    public void hiddenPopupWindow(){
        mView.clearAnimation();
        Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.slide_out);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                dismiss();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        mView.startAnimation(animation);
        setAnimation(180,0);

    }

    private void setAnimation(int start, int end) {
        mLlContent.clearAnimation();
        ValueAnimator valueAnimator = ValueAnimator.ofInt(start, end);

        valueAnimator.setDuration(300).start();
        valueAnimator.setTarget(mLlContent);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                getBackground().setAlpha((Integer) animation.getAnimatedValue());
            }
        });
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        mView.clearAnimation();
        mView.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.slide_in));
        setAnimation(0, 180);
        super.showAtLocation(parent, gravity, x, y);
    }
}















